﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using BioBooking.Data;

namespace BioBooking.Client.Screens
{
    /// <summary>
    /// Interaction logic for Member.xaml
    /// </summary>
    public partial class MemberScreen : UserControl
    {
        BioBooking.Data.IRepository service;
        ObservableCollection<Data.Member> listAllMembers;
        BioBooking.Data.Member selectedMember;

        ICollectionView collectionView;

        public MemberScreen()
        {
            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                service = Repository.GetRepository();

                listAllMembers = new ObservableCollection<Data.Member>(service.ListAllMembers());
                lbListMembers.DataContext = listAllMembers;

                collectionView = CollectionViewSource.GetDefaultView(listAllMembers);
                collectionView.Filter = new Predicate<object>(FilterCollection);
            }
        }

        public bool FilterCollection(object item)
        {
            Member member = item as Member;

            if (member == null)
                return false;

            return member.FullName.IndexOf(txtSearch.Text, StringComparison.CurrentCultureIgnoreCase) >= 0;
        }

        private void lbListMembers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Member member = lbListMembers.SelectedItem as Member;
            if (member == null)
                return;

            selectedMember = member;
        }

        private void butSearch_Click(object sender, RoutedEventArgs e)
        {
            collectionView.Refresh();
        }

        private void butSave_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void butOpdater_Click(object sender, RoutedEventArgs e)
        {
            service.UpdateMemberObject(selectedMember);
        }

        private void butCreateNew_Click(object sender, RoutedEventArgs e)
        {
            listAllMembers.Add(new Member() { FullName = "Unavngiven" });
            collectionView.Refresh();
            lbListMembers.SelectedIndex = (lbListMembers.Items.Count - 1);
        }

        private void butCount_Click(object sender, RoutedEventArgs e)
        {
            service.SetCounter(selectedMember.Counter, selectedMember.Id);
        }


    }
}
